package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.UserDetailVo;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.mvp.view.fragment.CommonDialog;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class MyInfo extends BaseActivity implements IView {
    @BindView(R.id.ll_top)
    RelativeLayout ll_top;
    @BindView(R.id.tv_center_title)
    TextView tv_center_title;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.img_real_name)
    ImageView img_real_name;

    private UserPresenter userPresenter;

    @Override
    protected int layoutResID() {
        return R.layout.activity_my_info;
    }

    @Override
    protected void initData() {
        userPresenter=new UserPresenter(this);
        tv_center_title.setText("个人中心");
        ImmersionBar.with(this).fitsSystemWindows(false).init();
        int height = ImmersionBar.getStatusBarHeight(this);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ll_top.getLayoutParams();
        layoutParams.setMargins(0, height, 0, 0);
        ll_top.setLayoutParams(layoutParams);
        getUserDetail();
    }


    @OnClick({R.id.img_back, R.id.rl_my_order, R.id.rl_my_car_num, R.id.rl_logout})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.rl_my_order:
                intent = new Intent(context, MyParkAndWashList.class);
                startActivity(intent);
                break;
            case R.id.rl_my_car_num:
                intent = new Intent(context, MyApplyAndCarsList.class);
                startActivity(intent);
                break;
            case R.id.rl_logout:

//                AlertDialog.Builder dialog = new AlertDialog.Builder(context);//通过AlertDialog.Builder创建出一个AlertDialog的实例
//
//                dialog.setTitle("退出");//设置对话框的标题
//                dialog.setMessage("确定退出当前账号");//设置对话框的内容
//                dialog.setCancelable(true);//设置对话框是否可以取消
//                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定按钮的点击事件
//
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        SPUtils.remove(context, ConstantValue.SpType.token);
//                        SPUtils.remove(context, ConstantValue.SpType.newtoken);
//                        SPUtils.remove(context, ConstantValue.SpType.phone);
//                        SPUtils.remove(context, ConstantValue.SpType.userid);
//                        ToastUtils.showShortToast("已经退出当前用户");
//                        MyInfo.this.finish();
//                    }
//                });
//                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {//取消按钮的点击事件
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        dialog.dismiss();
//                    }
//                });
//                dialog.show();//显示对话框
                loginOut();
                break;
        }
    }

    /**
     * 退出登录
     */
    private void loginOut() {
        CommonDialog loginOutDialog = CommonDialog.getInstance();
        loginOutDialog.show(getSupportFragmentManager(), "loginout");
        loginOutDialog.setClickCallback(new CommonDialog.OnClickCallback() {
            @Override
            public void cancel() {
                loginOutDialog.dismiss();
            }

            @Override
            public void confirm() {
                SPUtils.remove(context, ConstantValue.SpType.token);
                SPUtils.remove(context, ConstantValue.SpType.newtoken);
                SPUtils.remove(context, ConstantValue.SpType.phone);
                SPUtils.remove(context, ConstantValue.SpType.userid);
                ToastUtils.showShortToast("已经退出当前用户");
                loginOutDialog.dismiss();
                MyInfo.this.finish();
            }
        });
    }

    public void getSubscribes(int page, int pageSize) {
        userPresenter.getSubscribes(page, pageSize, ConstantValue.RequestKey.getSubscribes);
    }
    public void getUserDetail() {
        userPresenter.getUserDetail(ConstantValue.RequestKey.userinfo);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if (actionType.equals(ConstantValue.RequestKey.userinfo)) {
            UserDetailVo  userDetailVo = (UserDetailVo) data;
            if (userDetailVo != null) {
                if(userDetailVo.isHasRealNameAuth()){
                    img_real_name.setImageResource(R.drawable.real_name_icon);
                }else{
                    img_real_name.setImageResource(R.drawable.real_name_no_icon);
                }
                tv_phone.setText(userDetailVo.getCellphone().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
                SPUtils.putString(context,ConstantValue.SpType.phone,userDetailVo.getCellphone());
            }
        }
    }

    @Override
    public void onFailed(String code, String msg) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserDetail();
    }
}
